<?php

namespace frontend\modules\resource\controllers;

use frontend\modules\common\behavior\DisableCsrf;
use frontend\modules\common\controllers\BaseController;
use frontend\modules\resource\logic\ResourceClassesLogic;
use Yii;


/**
 * ResourceClassesController implements the CRUD actions for ResourceClasses model.
 */
class BranchClassesController extends BaseController
{

    public function behaviors()
    {
        return [
            'csrf' => [
                'class' => DisableCsrf::className(),
                'controller' => $this,
                'actions' => [
                    'change-cate'
                ]
            ]
        ];
    }

    /**
     * @throws \yii\base\ExitException
     */
    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $this->checkBrId('branchIdForBranchClasses');
    }

    /**
     * 渲染选择分类页
     */
    public function actionIndex()
    {

        return $this->render('index');
    }

    /**
     * @return string
     * @throws \yii\base\ExitException
     */
    public function actionCateList()
    {

        //获取所有分类 和 该店铺的所选分类
        $data = (new ResourceClassesLogic())->getBranchCate($this->branchId);
        return $this->suc(['data' => $data]);

    }


    /**
     * @return string
     * @throws \yii\base\ExitException
     * @throws \yii\db\Exception
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionChangeCate()
    {

        $ids = Yii::$app->request->post('id');
        $ids = json_decode($ids, true);
        //获取所有分类 和 该店铺的所选分类
        $data = (new ResourceClassesLogic())->changeBranchClasses($ids, $this->branchId);
        return $this->suc(['data' => $data, 'msg' => '保存成功']);
    }
}
